﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using control.Biz;

namespace control
{
    public partial class frmPhieuNhap : Form
    {
        List<BizSach> saches = new List<BizSach>();
        BizPhieuNhap phieunhap = new BizPhieuNhap();

        public frmPhieuNhap()
        {
            InitializeComponent();
        }
        public frmPhieuNhap(BizPhieuNhap pn)
        {
            InitializeComponent();
            phieunhap = pn;
            dateTimePicker1.Value = phieunhap.NgayNhap;
            dateTimePicker2.Value = phieunhap.NgayNhap;
            textBox1.Text = phieunhap.Ma;
            dataGridView2.AutoGenerateColumns = false;
            bindGridview2();
        }
        private void frmPhieuNhap_Load(object sender, EventArgs e)
        {
            saches = BizSach.GetAll();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;
            dataGridView1.DataSource = saches;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                BizSach sach = saches[row.Index];
                BizChiTietPhieuNhap item = new BizChiTietPhieuNhap()
                {
                    PhieuNhap = phieunhap,
                    Sach = sach,
                };
                if (phieunhap.ItemsContains(item) == false)
                    phieunhap.Items.Add(item);
            }
            bindGridview2();
        }

        private void bindGridview2()
        {
            dataGridView2.DataSource = null;
            dataGridView2.DataSource = phieunhap.Items;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                BizSach sach = saches[row.Index];
                BizChiTietPhieuNhap item = new BizChiTietPhieuNhap()
                {
                    PhieuNhap = phieunhap,
                    Sach = sach,
                };
                if (phieunhap.ItemsContains(item)==false)
                    phieunhap.Items.Add(item);
            }
            bindGridview2();
        }

        private void xoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView2.SelectedRows)
            {
                phieunhap.Items.RemoveAt(row.Index);
            }
            bindGridview2();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "")
                errorProvider1.SetError(textBox1, "Xin nhạp mã phiếu");
            else
            {
                if (phieunhap.ID == 0)
                {
                    phieunhap.NgayNhap = dateTimePicker1.Value;
                    phieunhap.Ma = textBox1.Text;
                    try
                    {
                        phieunhap.Insert();
                        this.Close();
                    }
                    catch { errorProvider1.SetError(textBox1, "mã phiếu bị trùng"); }

                }
                else
                {
                    phieunhap.Ma = textBox1.Text;
                    try
                    {
                        phieunhap.Update();
                        this.Close();
                    }
                    catch { errorProvider1.SetError(textBox1, "mã phiếu bị trùng"); }

                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string st = textBox2.Text;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Selected = false;
                if (saches[row.Index].MaSach.Contains(st) || saches[row.Index].TenSach.Contains(st))
                    row.Selected = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (phieunhap.Items.Count != 0)
            {
                string st = textBox3.Text;
                foreach (DataGridViewRow row in dataGridView2.Rows)
                {
                    row.Selected = false;
                    if (phieunhap.Items[row.Index].Sach.MaSach.Contains(st) || phieunhap.Items[row.Index].Sach.TenSach.Contains(st))
                        row.Selected = true;
                }
            }
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            ValidateName();
        }
        private bool ValidateName()
        {
            bool bStatus = true;
            if (textBox1.Text == "")
            {
                errorProvider1.SetError(textBox1, "xin nhập mã phiếu");
                bStatus = false;
            }
            else
                errorProvider1.SetError(textBox1, "");
            return bStatus;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            BizSach sach = new BizSach();
            sach.IdSach = -1;
            FrmSach frm = new FrmSach(sach);
            frm.ShowDialog();
            bindGridview1();
        }

        private void bindGridview1()
        {
            saches = BizSach.GetAll();
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = saches;
        }

    }
}
